<?php
namespace Home\Controller;
use Think\Controller;

class YzmController extends CommonController {
    public function index(){
        $this->display();
    }
    public function index1(){
        $this->display();
    }
    /**
     * 发起二次校b25384ea5b4c4d10ac879889a7dde99c 验请求
     * @param $validate 二次校验数据
     * @param $user 用户信息
     */

    public function verifyy($validate, $user) {
        $params = array();
        $params["captchaId"] = 'b25384ea5b4c4d10ac879889a7dde99c';
        $params["validate"] = $validate;
        $params["user"] = $user;
        // 公共参数
        $params["secretId"] = '64e7487ba293a4c5013e8e32d284fc64';
        $params["version"] = 'v2';
        $params["timestamp"] = sprintf("%d", round(microtime(true)*1000));// time in milliseconds
        $params["nonce"] = sprintf("%d", rand()); // random int
        $params["signature"] = $this->sign('11d506f8f7d3c60d42437f24d1641f0c', $params);

        //var_dump($params);
        $result = $this->send_http_request($params);
        return array_key_exists('result', $result) ? $result['result'] : false;
    }

    /**
     * 计算参数签名
     * @param $secret_key 密钥对key
     * @param $params 请求参数
     */
    public function sign($secret_key, $params){
        ksort($params); // 参数排序
        $buff="";
        foreach($params as $key=>$value){
            $buff .=$key;
            $buff .=$value;
        }
        $buff .= $secret_key;
        return md5($buff);
    }

    /**
     * 发送http请求
     * @param $params 请求参数
     */
    public function send_http_request($params){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://c.dun.163yun.com/api/v2/verify');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, YIDUN_CAPTCHA_API_TIMEOUT);
        curl_setopt($ch, CURLOPT_TIMEOUT, YIDUN_CAPTCHA_API_TIMEOUT);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));

        /*
         * Returns TRUE on success or FALSE on failure.
         * However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
         */
        $result = curl_exec($ch);
        // var_dump($result);

        if(curl_errno($ch)){
            $msg = curl_error($ch);
            curl_close($ch);
            return array("error"=>500, "msg"=>$msg, "result"=>false);
        }else{
            curl_close($ch);
            return json_decode($result, true);
        }
    }
    public function test(){
        header("Access-Control-Allow-Origin: *");
        session_start();
        $validate = $_POST['NECaptchaValidate']; // 获得验证码二次校验数据
        if(get_magic_quotes_gpc()){// PHP 5.4之前默认会将参数值里的 \ 转义成 \\，这里做一下反转义
            $validate = stripcslashes($validate);
        }
        $user = "{'user':123456}"; // 当前用户信息，值可为空

        $result = $this->verify($validate, $user);
        var_dump($result);
    }
    public function get_id(){
        header("Access-Control-Allow-Origin: *");
        $get_id = 'b25384ea5b4c4d10ac879889a7dde99c';
        //echo json_encode(['data'=>$data]);
        $data['ret'] = '1001';
        $data['code'] = 'success';
        $data['data']=$get_id;
        $data1 = $this->jiami($data);
        $this->return = ['data' => $data1];
        return true;
    }
    public function test1(){
        session_start();
//1. 获取到用户提交的验证码
        $captcha = $_POST["captcha"];
//2. 将session中的验证码和用户提交的验证码进行核对,当成功时提示验证码正确，并销毁之前的session值,不成功则重新提交
        if(strtolower($_SESSION["captchaimg"]) == strtolower($captcha)){
            echo "验证码正确!";
            $_SESSION["captcha"] = "";
        }else {
            echo "验证码提交不正确!";
        }
    }
    public function test2(){
//        phpinfo();die;
        session_start();
        ob_clean();
        $ss = I('ss');
        //1.创建黑色画布
        $image = imagecreatetruecolor(100, 30);
        //2.为画布定义(背景)颜色
        $bgcolor = imagecolorallocate($image, 255, 255, 255);
        //3.填充颜色
        imagefill($image, 0, 0, $bgcolor);
        // 4.设置验证码内容
        //4.1 定义验证码的内容
        $content = "0123456789";
        //4.1 创建一个变量存储产生的验证码数据，便于用户提交核对
        $captcha = "";
        for ($i = 0; $i < 3; $i++) {
            // 字体大小
            $fontsize = 10;
            // 字体颜色
            $fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
            // 设置字体内容
            $fontcontent = substr($content, mt_rand(0, strlen($content)), 1);
            $captcha .= $fontcontent;
            // 显示的坐标
            $x = ($i * 100 / 4) + mt_rand(5, 10);
            $y = mt_rand(5, 10);
            // 填充内容到画布中
            imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
        }
//        $_SESSION["captcha"] = strtolower($captcha);
        $ip=get_client_ip();
        $redis = new \Redis();
        $redis->pconnect('r-bp1476c19e907754.redis.rds.aliyuncs.com', 6379);
        $redis->auth('Ruian0571');
        $redis->set('captcha'.$ss,$captcha);

//        var_dump($secode);
//        var_dump($id);
//        $redis->set('aa',json_encode($secode));
        $redis->expire('captcha',600);
//        var_dump($captcha);die;
        //4.3 设置背景干扰元素
        for ($$i = 0; $i < 200; $i++) {
            $pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointcolor);
        }
        //4.4 设置干扰线
        for ($i = 0; $i < 3; $i++) {
            $linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $linecolor);
        }
        //5.向浏览器输出图片头信息
        header('content-type:image/png');
        //6.输出图片到浏览器
        imagepng($image);
        //7.销毁图片
        imagedestroy($image);
    }
}